package com.gugee.ins.data.task.mark.service;

import com.gugee.ins.common.dal.service.mark.InsMarkBloggerBgsMonthDao;
import com.gugee.ins.common.dal.service.mark.InsRkBloggerBgsMonthDao;
import com.gugee.ins.common.dto.RankTaskParam;
import com.xiguaji.boot.toolkit.DateTimeUtils;
import com.xxl.job.core.log.XxlJobLogger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.Calendar;
import java.util.List;


/**
 * ins 带货播主排行表
 * @author huangxin
 */
@Component
@Slf4j
public class InsRkBloggerBgsMonthSvc extends AbstractMarkRankProducerHandler {

    /**
     *
     */
    @Autowired
    InsRkBloggerBgsMonthDao insRkBloggerBgsMonthDao;

    /**
     *
     */
    @Autowired
    InsMarkBloggerBgsMonthDao insMarkBloggerBgsMonthDao;

    /**
     * 处理排行表
     */
    @Override
    public void handleRank(RankTaskParam taskParam) {
        // 查找一级分类
        List<String> categoryList = getCategory(taskParam.getCategory(),true);

        Calendar calendar = Calendar.getInstance();
        // 设置时间
        if(!StringUtils.isEmpty(taskParam.getDateCode())){
            calendar.setTimeInMillis(getDateTime(taskParam.getDateCode()));
        }
        calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        int rankMonth = Integer.parseInt(DateTimeUtils.formatSecond(calendar.getTimeInMillis() / 1000,
                "yyyyMMdd",taskConfig.getInsBloggerMarkDaytimeZone()));

        calendar.set(Calendar.DAY_OF_MONTH,1);
        calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1);
        int lastDateCode = Integer.parseInt(DateTimeUtils.formatSecond(calendar.getTimeInMillis() / 1000,
                "yyyyMM",taskConfig.getInsBloggerMarkDaytimeZone()));

        // 分类的榜单
        if(categoryList != null && !categoryList.isEmpty()){
            for(String category : categoryList){
                markRankMonth(rankMonth,lastDateCode,category);
            }
        }

        // 删除前个月的无用数据
        deleteMarkWeek(calendar);
    }

    /**
     *
     * @param dateCode
     * @param category
     */
    public void markRankMonth(int dateCode,int lastDateCode,String category){
        try{
            insRkBloggerBgsMonthDao.insertRk(dateCode,lastDateCode,category,taskConfig.getInsRkBloggerBgsRankCount());
        }catch (Exception e){
            log.info("ins 带货播主月榜单 生成失败 date:{},category:{},msg:{}", dateCode,category,e.getMessage());
            XxlJobLogger.log("ins 品牌月榜单 生成失败 date:{},category:{},msg:{}",dateCode,category,e.getMessage());
        }
    }

    /**
     *
     */
    public void deleteMarkWeek(Calendar calendar){
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        int dateCode =  Integer.parseInt(DateTimeUtils.formatSecond(calendar.getTimeInMillis() / 1000,
                "yyyyMMdd",taskConfig.getInsBloggerMarkDaytimeZone()));
        try{
            insMarkBloggerBgsMonthDao.deleteByDateCode(dateCode);
        }catch (Exception e){
            log.info("ins 删除带货播主月打点数据失败 date:{},msg:{}", dateCode,e.getMessage());
            XxlJobLogger.log("ins 删除带货播主月打点数据失败 date:{},msg:{}",dateCode,e.getMessage());
        }
    }
}
